package com.atguigu.lease.web.admin.controller;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.RoomInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 李壮
 * @CreateTime: 2025-05-11
 * @Version: 1.0
 */

@RestController
@RequestMapping("/admin/room")
@Tag(name = "房间信息管理")
public class RoomInfoController {

    @Autowired
    RoomInfoService roomInfoService;

    @Operation(summary = "根据id修改房间发布状态")
    @PostMapping("/updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, ReleaseStatus status){
        LambdaUpdateWrapper<RoomInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(RoomInfo::getId,id);
        lambdaUpdateWrapper.set(RoomInfo::getIsRelease,status);
        roomInfoService.update(lambdaUpdateWrapper);
        return Result.ok();
    }

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo submitVo){

        roomInfoService.saveOrUpdateRoom(submitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("/pageItem")
    public Result pageItem(@RequestParam Long current, @RequestParam Long size, RoomQueryVo roomQueryVo){
        IPage<RoomItemVo> page = new Page<>(current,size);
        IPage<RoomItemVo> result = roomInfoService.PageRoomItemByQuery(page,roomQueryVo);
        return Result.ok(result);

    }

    @Operation(summary = "根据ID获取房间详细信息")
    @GetMapping("/getDetailById")
    public Result getDetailById(@RequestParam Long id){
        RoomDetailVo roomDetailVo = roomInfoService.getRoomDetailById(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id){
        roomInfoService.removeRoomById(id);
        return Result.ok();
    }

    @Operation(summary = "根据公寓id查询房间列表")
    @GetMapping("listBasicByApartmentId")
    public Result listBasicByApartmentId(@RequestParam Long id){
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        lambdaQueryWrapper.eq(RoomInfo::getIsRelease,ReleaseStatus.RELEASED);
        List<RoomInfo> roomInfoList = roomInfoService.list(lambdaQueryWrapper);

        return Result.ok(roomInfoList);

    }
}
